package app.server.project.main.service.service.upay.tron;

import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentLinkedDeque;

@RequiredArgsConstructor
@Service
public class EventService {

    private final ConcurrentLinkedDeque<Object> deque = new ConcurrentLinkedDeque<>();

    private final ApplicationEventPublisher publisher;

    public void add(Object obj) {
        deque.add(obj);
    }

    @Scheduled(fixedDelay = 2 * 1000)
    public void consume() {
        var data = deque.poll();
        if (data != null) {
            publisher.publishEvent(data);
        }
    }
}
